<?php

namespace common\models\es\dataProvider\comProducts;


use Yii;

class ListProducts extends Query
{
    public function getData()
    {
        $params = [
            'index' => $this->getIndexName(),
            'body' => $this->getBody()
        ];
//        print_r(json_encode($params['body'])); exit;
        $esResult = Yii::$app->elasticsearch->search($params);
        $count = $esResult['hits']['total']['value'] ?? 0;
        $list = $esResult['hits']['hits'] ?? [];
        return [$list, $count];
    }

    /**
     * 拼装请求体
     * @return array
     */
    protected function getBody()
    {
        $return = [
            'track_total_hits' => true,
            '_source' => $this->fields,
            'size' => $this->size,
            'from' => $this->from,
            'query' => $this->getQueryBody(),
            'sort' => $this->sort
        ];
        if (isset($this->query['title']) && $this->query['title']) {
            $return['highlight'] = ['fields' => ['title' => (object)[]]];
        }
        return $return;
    }

    protected function getQueryBody()
    {
        return [
            'bool' => [
                'filter' => $this->getFilter()
            ],
        ];
    }

    /**
     * 拼装过滤条件
     * @return array
     */
    protected function getFilter(): array
    {

        $filter = [
            ['term' => ['is_delete' => 0]],//未删除
        ];

        //根据商品主键ID
        if (isset($this->query['id']) && !empty($this->query['id'])) {
            $filter[] =
                ['terms' => ['id' => $this->query['id']]]
            ;
        }

        //商品ID
        if (isset($this->query['ids']) && !empty($this->query['ids'])) {
            $filter[] = [
                "terms" => ['id' => $this->query['ids']]
            ];
        }
        //用户ID
        if (isset($this->query['uid']) && !empty($this->query['uid'])) {
            $filter[] =
                ['term' => ['uid' => $this->query['uid']]]
            ;
        }




        //根据商品名称搜索关键词
        if (isset($this->query['title']) && !empty($this->query['title'])) {
            $filter[] = [
                'bool' => [
                    'must' => [
                        'wildcard' => ['title' => '*' . $this->query['title'] . '*'],
                    ]
                ]
            ];
//            $filter[] = [
//                ['match' => ['title' => $this->query['title']]]
//            ];
        }

        //根据违禁词搜索
        if (isset($this->query['words']) && !empty($this->query['words'])) {
            $words = [];
            foreach ($this->query['words'] as $k => $v) {
                if($v=='?'||$v=='\\'){
                    $v = "\\".$v;
                }
                $goods_info =
                    array(
                        'wildcard' => array(
                            'title' => '*' . $v . '*'
                        )
                    );
                $words[] = $goods_info;
            }

            $filter[] = [
                'bool' => [
                    'should' => $words
                ]
            ];
        }
        //商品ID
        if (isset($this->query['num_iid']) && !empty($this->query['num_iid'])) {
            $filter[] = [
                "terms" => ['num_iid' => $this->query['num_iid']]
            ];
        }

        //商品来源平台ID
        if (isset($this->query['platform']) && !empty($this->query['platform'])) {
            $filter[] = [
                "term" => ['platform' => intval($this->query['platform'])]
            ];
        }

        //仓库ID
        if (isset($this->query['store_id']) && !empty($this->query['store_id'])) {
            $filter[] = [
                "term" => ['store_id' => $this->query['store_id']]
            ];
        }

        //复制状态
        if (isset($this->query['copy_status']) && !empty($this->query['copy_status'])) {
            if ($this->query['copy_status']) {
                $filter[] = [
                    "range" => ["copy_times" => ['gt' => 0]]
                ];
            } else {
                $filter[] = [
                    "term" => ['copy_times' => 0]
                ];
            }
        }

        //备注信息
        if (isset($this->query['remark']) && !empty($this->query['remark'])) {
            $filter[] = [
                ['match' => ['remark' => $this->query['remark']]]
            ];
        }

        /**
         * 价格区间检索
         */
        if (isset($this->query['min_price']) || isset($this->query['max_price'])) {
            $priceTep = [];
            if (isset($this->query['min_price']) && !empty($this->query['min_price'])) {
                $priceTep['from'] = $this->query['min_price'];
            }

            if (isset($this->query['max_price']) && !empty($this->query['max_price'])) {
                //截止时间加上一天的时间戳
                $priceTep['to'] = $this->query['max_price'];
            }
            if (count($priceTep)) {
                $filter[] = [
                    "range" => [
                        'prices' => $priceTep
                    ]
                ];
                unset($priceTep);
            }
        }

        /**
         * 时间区间检索
         */
        if (isset($this->query['start_time']) || isset($this->query['end_time'])) {
            $timeTep = [];
            if (isset($this->query['start_time']) && !empty($this->query['start_time'])) {
                $timeTep['from'] = $this->query['start_time'];
            }

            if (isset($this->query['end_time']) && !empty($this->query['end_time'])) {
                //截止时间加上一天的时间戳
                $timeTep['to'] = $this->query['end_time'];
            }
            if (count($timeTep)) {
                $filter[] = [
                    "range" => [
                        'created_at' => $timeTep
                    ]
                ];
                unset($timeTep);
            }
        }
        return $filter;
    }

}
